/***********************************************************************
** Copyright (C) 2011 Anthony Naddeo <anthony.naddeo@gmail.com>
**
** This file is part of LinkedUp
**
** LinkedUp is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** LinkedUp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this package; If not, see <http://www.gnu.org/licenses/>.
**
***********************************************************************/
import QtQuick 1.1
import com.meego 1.0
import "components"

MyPage {
	id: loginScreen
	anchors.fill: parent
	title: "Authorize"

	tools: commonTools

	signal authorized
//	onAuthorized: {
//		loginScreen.destroy()
//	}

	Component.onCompleted: get(OAuth.request_token())
	onGetFinished: {
		OAuth.generate_access_url(responseText)
	}

	function authorize(url){
		loginScreen.busy = true
		var xmlHttp = new XMLHttpRequest();
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				loginScreen.busy = false
				console.log(xmlHttp.status)
				console.log(xmlHttp.statusText)
				console.log(xmlHttp.responseText)
				if(xmlHttp.status == "200"){
					authorized()
					OAuth.store_key(xmlHttp.responseText)
				}

			}
		}
		xmlHttp.open( "GET", url, true );
		xmlHttp.send( null );
	}


	Column{
		anchors.centerIn: parent
		spacing: 30
		Label {
			text: "Enter the pin code you receive below"
			horizontalAlignment: Text.AlignLeft
			anchors.horizontalCenter: parent.horizontalCenter
			font.pixelSize: 22
		}

		TextField{
			id: textField
			anchors.horizontalCenter: parent.horizontalCenter
			inputMethodHints: Qt.ImhDigitsOnly | Qt.ImhNoPredictiveText
			validator: RegExpValidator{regExp: /[0-9][0-9][0-9][0-9][0-9]/}
		}

		Button{
			text: "Authorize"
			onClicked: authorize(OAuth.access_token(textField.text))
			anchors.horizontalCenter: parent.horizontalCenter
		}
	}







}
